package com.mark.web.stars.controller;

import com.github.pagehelper.PageInfo;
import com.mark.common.ServerResBase;
import com.mark.common.ServerResponse;
import com.mark.web.stars.entity.from.AddProperNounFrom;
import com.mark.web.stars.entity.vo.ProperNounVO;
import com.mark.web.stars.service.IProperNounService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: 专有名词控制器
 * @Author: Mark
 * @CreateDate: 2020/5/23 18:52
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Api(tags = "星运-装有名词模块")
@RestController
@RequestMapping(value = "/stars/properNoun/")
public class ProperNounController {

    @Autowired
    private IProperNounService iProperNounService;

    @ApiOperation(value = "获取字典类型标签")
    @GetMapping(value = "getProperNounDictTypeList")
    public ServerResponse<List<Map<String,Object>>> getProperNounDictTypeList(){
        List<Map<String, Object>> model = iProperNounService.getProperNounDictTypeList();
        return ServerResponse.createBySuccess(model);
    }

    @ApiOperation(value = "获取搜索条件内的专有名词信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前的页数", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", defaultValue = "50"),
            @ApiImplicitParam(name = "content", value = "模糊搜索的专有名词")
    })
    @PostMapping(value = "getProperNounList")
    public ServerResponse<PageInfo<ProperNounVO>> getProperNounList(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                                    @RequestParam(value = "pageSize", defaultValue = "50") int pageSize,
                                                                    @RequestParam(value = "content") String content) {
        PageInfo<ProperNounVO> model = iProperNounService.getProperNounList(pageNum, pageSize, content);
        return ServerResponse.model(model);
    }

    @ApiOperation(value = "添加一条专有名词记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "专有名词"),
            @ApiImplicitParam(name = "description", value = "专有名词的描述，该字段可以为空"),
    })
    @PostMapping(value = "add")
    public ServerResBase add(@Valid AddProperNounFrom addProperNounFrom, BindingResult result) {
        if(result.hasErrors()){
            return ServerResponse.createByIllegalArgument(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        iProperNounService.add(addProperNounFrom.getContent(),
                addProperNounFrom.getDescription(),
                addProperNounFrom.getCheckedTypeIdList());
        return ServerResponse.createBySuccess();
    }

    @ApiOperation(value = "删除一条专有名词记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "专有名词记录id")
    })
    @PostMapping(value = "delete")
    public ServerResBase delete(@RequestParam(value = "id") int id){
        if(id == 0 || id < 0){
            return ServerResponse.createByIllegalArgument("输入的id应该大于0");
        }
        iProperNounService.delete(id);
        return ServerResponse.createBySuccess();
    }

}
